/*!
\file DCommonData.cpp
\brief Contains implementation of DCommonData.
\ingroup DOT
*/

#include"DCommonData.h"

DCommonData::DCommonData(){

}

DCommonData::~DCommonData(){

}

void DCommonData::_setOptions( QString options ){
	QStringList optionParts = options.split( "," );
	for( int i = 0; i < optionParts.size(); i++ ){
		while( optionParts[i].count("\"")%2 && optionParts.size() > i+1 )
			optionParts[i] = optionParts[i].append(optionParts.takeAt(i+1));
			
		QStringList help = optionParts[i].split("=");
		
		if( help[0].trimmed() == "color" ){
			help.pop_front();
			m_colourStack.push_front(help.join("=").trimmed());
		}else{
			m_options.push_back( help[0].trimmed() );
			help.pop_front();
			m_values.push_back(help.join( "=" ).trimmed());
		}
	}
}

QString DCommonData::_dotOptions()const{
	QString ret;
	if( m_options.size() > 0 ){
		ret.append(" [");
		for( int i = 0; i < m_options.size(); i++ ){
			ret.append( m_options[i] + QString("=") + m_values[i] );
			if( i+1 < m_options.size() )
				ret.append(" ,");
		}
		if( m_colourStack.size() > 0 ){
			ret.append(" ,");
			ret.append("color=\"");
			ret.append(m_colourStack[0]);
			ret.append("\"");
		}
		ret.append( "]" );
	}else if( m_colourStack.size() > 0 ){
		ret.append(" [");
		ret.append("color=\"");
		ret.append(m_colourStack[0]);
		ret.append("\"");
		ret.append( "]" );
	}
	return ret;
}

void DCommonData::setOption( QString option, QString value ){
	bool found = false;
	for( int i = 0; i < m_options.size() && !found; i++ ){
		if( m_options[i].trimmed() == option.trimmed() ){
			m_values[i] = value;
			found = true;
		}
	}
	
	if( !found ){
		if( option.trimmed() == "color" ){
			if( value.trimmed().isEmpty() ){
				if( m_colourStack.size() > 0 )
					m_colourStack.pop_front();
			}else
				m_colourStack.push_front(value.trimmed());
		}else{
			m_options.push_back( option );
			m_values.push_back( value );
		}
	}
}

QString DCommonData::getOption( QString option )const{
	if( option.trimmed() == "color" )
		return (m_colourStack.size()>0) ? m_colourStack[0] : QString();
	
	
	for( int i = 0; i < m_options.size(); i++ ){
		if( m_options[i].trimmed() == option.trimmed() )
			return m_values[i];
	}
	return QString();
}
